<%#
 Copyright 2020-2022 sirpdboy Wich <sirpdboy@qq.com>
 https://github.com/sirpdboy/netspeedtest
 Licensed to the public under the Apache License 2.0.
-%>

<%+cbi/valueheader%>
<%local fs=require"nixio.fs"%>
<input type="button" class="btn cbi-button cbi-button-apply" id="apply_run_button" value="<%:Click to execute%>" onclick=" return apply_run(this) "/>
<div id="logview" style="display:none">
<input type="checkbox" id="reversetag" value="reverse" onclick=" return reverselog()" style="vertical-align:middle;height: auto;"><%:reverse%></input>
<textarea id="cbid.logview.test.conf" class="cbi-input-textarea" style="width: 100%;display:block;" data-update="change" rows="20" cols="80" readonly="readonly" > </textarea>
</div>
<script type="text/javascript">//<![CDATA[
	const RUN_URL = '<%=luci.dispatcher.build_url("admin", "network", "netspeedtest","speedtestwanrun")%>';
    const S_URL = '<%=luci.dispatcher.build_url("admin", "network", "netspeedtest","netcheck")%>';
var checkbtn = document.getElementById('apply_run_button');

var islogreverse = false;
function reverselog(){
	var lv = document.getElementById('cbid.logview.test.conf');
	lv.innerHTML=lv.innerHTML.split('\n').reverse().join('\n')
	if (islogreverse){
	islogreverse=false;
	}else{
	islogreverse=true;
	}
	return
}
function apply_run(btn) {

	var sid='speedtestwan'
	var opt={
    		base:"cbid.netspeedtest."+sid,
    		get:function(opt){
    			var id=this.base+'.'+opt;
    			var obj=document.getElementsByName(id)[0] || document.getElementsByClassName(id)[0] || document.getElementById(id)
    			if (obj){
    				return obj;
    			}else{
    				return null;
    			}
    		},
    		getlist:function(opt){
    			var id=this.base+'.'+opt;
    			var objs=document.getElementsByName(id) || document.getElementsByClassName(id);
    			var ret=[];
    			if (objs){
    				for (var i=0;i < objs.length;i++){
    					ret[i]=objs[i].value;
    				}
    			}else{
    				alert("<%:Fatal on get option,please help in debug%>:"+opt);
	    		}
	    		return ret;
    		}
    }
	btn.value='<%:Waiting (executing)...%>';
	btn.disabled=true;
    var cli=opt.get("speedtest_cli").value;
	console.log(cli);
        XHR.get(RUN_URL, { cli: cli	},
		(x) =>{}
	);
	poll_check();
	return;
}

function poll_check(){
var tag = document.getElementById('logview');
tag.style.display="block"
XHR.poll(3, '<%=url([[admin]], [[network]], [[netspeedtest]], [[netcheck]])%>', null,
	function(x, data) {
		var lv = document.getElementById('cbid.logview.test.conf');

		if (x.responseText && lv) {
			if (x.responseText=="\u0000"){
				for(j = 0,len=this.XHR._q.length; j < len; j++) {
					if (this.XHR._q[j].url == '<%=url([[admin]], [[network]], [[netspeedtest]], [[netcheck]])%>'){
						this.XHR._q.splice(j,1);
						checkbtn.disabled = false;
						checkbtn.value    = '<%:Click to execute%>';
						break;
					}
				}
				return	
			}
			if (islogreverse){
				lv.innerHTML = x.responseText.split('\n').reverse().join('\n')+lv.innerHTML;
			}else{
				lv.innerHTML += x.responseText;
		          }
		}
	}
);}

//]]>
</script>
<%+cbi/valuefooter%>
